package info.wegosoft.android.adapter;

import java.util.Date;
import java.util.List;

import info.wegosoft.R;
import info.wegosoft.android.model.Comment;
import info.wegosoft.android.util.TimeUtil;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**  
 * Filename:    CommentArrayAdapter.java 
 * Package:     info.wegosoft.android.adapter 
 * Description: TODO(这里用一句话描述这个类的作用)
 * Copyright:   Copyright (c) wegosoft.info 2011  
 * @author:     李辉 
 * email:       fzlihui@gmail.com
 * @version:    1.0  
 * Create at:   2011-6-23 下午05:20:49  
 */
public class CommentAdapter extends BaseAdapter {

	private List<Comment> commentList;
	private Context context;
	private int resource;
	
	public CommentAdapter(Context context, int resource,
			List<Comment> list) {
		
		this.context = context;
		
		this.resource = resource;
		
		this.commentList = list;
		
	}
	
	public Context getContext() {
		return context;
	}

	public int getCount() {
		
		return commentList.size();
		
	}

	public Object getItem(int position) {

		return commentList.get(position);
		
	}

	public long getItemId(int position) {
		
		return position;
		
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		LinearLayout tableView = null;
		
		if (convertView == null) {
			
			tableView = new LinearLayout(getContext());

			String inflater = Context.LAYOUT_INFLATER_SERVICE;

			LayoutInflater vi = (LayoutInflater) context.getSystemService(
					inflater);

			vi.inflate(resource, tableView, true);

		} else {
			
			tableView = (LinearLayout) convertView;
		
		}

		Comment comment = (Comment) getItem(position);
		
		TextView userName = (TextView) tableView.findViewById(R.id.userName);
		
		userName.setText(comment.getUserName());
		
		TextView commentTime = (TextView) tableView.findViewById(R.id.commentTime);
			
		commentTime.setText(TimeUtil.timeBetween(comment.getCreate_at(), new Date()));
		
		TextView commentContent = (TextView) tableView.findViewById(R.id.commentContent);
		
		commentContent.setText(comment.getText());
		
		return tableView;
	}

	public void update(List<Comment> list){
		
		this.commentList = list;
		
		this.notifyDataSetChanged();
		
	}
}
